#ifndef SEGMENT_H_
#define SEGMENT_H_

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "utils.h"
#include "conf.h"

const int FILLFACTOR = 3;
const int MAX_WORDS_DIC_LINE = 400000;
const int MAX_WORDS_PER_CHARACTER = 15000;
const int MAX_BUFFER_SIZE = 1024*1024;  // 1m
const int MAX_WORD_SIZE = 31;

struct SegRes {
  std::string word;
  char type;
  int times;
};

class Segment {

public:
  Segment();
  ~Segment();

  //static Segment& get_instance();
  int segment(const char* ttext, std::vector<SegRes>& result);
  //void release() { delete instance_; }
  
private:
  int init_stop_words();
  int init_t2s();
  bool is_stop_word(const std::string& word);
  bool convert_word(const char* oword, char* newword);
  int convert_sentence(char* sentence);
  void create_dict();
  int describe(const char* tword);
  void do_segment(const char* word, char type = ' ');


private:
  struct word {
    std::string value;
    bool has_more;
    bool is_word;
    int max_word_length;
    word* next;
  };

  struct character {
    word* word_address;
    int hash_size;
  };

  //static Segment* instance_;
  int first_max_;
  int first_min_;
  int second_min_;
  int second_gap_;
  int all_words_sum_;
  unsigned int single_word_num_;

  character* baseaddr_;
  char* buffer_;
  char* temp_ch_;
  char* text_;

  std::set<std::string> stop_words_set_;
  std::map<std::string, std::pair<char, int> > map_res_;
  std::string stop_word_file_;
  std::string t2s_file_;
  std::string dict_file_;
  std::map<std::string, std::string> map_t2s_;

};

#endif
